package com.travel.server.manager.impl.command.customer;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.customer.UpdateMemberGradeInfoRequest;
import com.travel.server.manager.dto.response.customer.UpdateMemberGradeInfoResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.MemberGradeDal;
import com.travel.server.manager.model.MemberGradeInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 18:08
 * @Desc:
 */
@Component
@Slf4j
public class UpdateMemberGradeInfoCommand extends AbstractCommand<UpdateMemberGradeInfoRequest, UpdateMemberGradeInfoResponse> {

    @Autowired
    private MemberGradeDal memberGradeDal;

    @Override
    protected void checkInput(UpdateMemberGradeInfoRequest request) {

    }

    @Override
    public UpdateMemberGradeInfoResponse handle(UpdateMemberGradeInfoRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        Integer id = request.getId();

        MemberGradeInfo memberGradeInfo;
        if(id == null){
            memberGradeInfo = new MemberGradeInfo();
            memberGradeInfo.setCreateBy(userInfo.getName());
            memberGradeInfo.setCreateTime(now);
            memberGradeInfo.setState(CommonConstant.YES);
        }else{
            memberGradeInfo = memberGradeDal.selectById(id);
        }
        memberGradeInfo.setName(request.getGradeName());
        memberGradeInfo.setIsDefault(request.getIsDefault());
        memberGradeInfo.setScoreLimit(request.getScoreLimit());
        memberGradeInfo.setMemberRight(request.getMemberRight());
        memberGradeInfo.setUpdateBy(userInfo.getName());
        memberGradeInfo.setUpdateTime(now);

        id = memberGradeDal.insertOrUpdate(memberGradeInfo);

        UpdateMemberGradeInfoResponse response = new UpdateMemberGradeInfoResponse();
        response.setId(id);
        return response;
    }
}
